package terra.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.GUIContext;

import terra.managers.DefManager;
import terra.managers.ResourceManager;
import terra.model.ShortCut;
import terra.slick.GameState;
import terra.ui.wnd.AbstractWindow;

/**Класс, представляющий окно панели быстрого доступа. Содержит в себе список предметов, которыми можно строить.
 * TODO в быстрый доступ можно ложить все предметы и действия, с интерфейсом Usable  */
public class QuickBar extends AbstractWindow {

	private boolean horisontal = true;
	private Rectangle button = new Rectangle(0, 0, 16, 16);
	public Color back = new Color(Color.gray.getRed(),Color.gray.getGreen(),Color.gray.getBlue(),0.75f);

	public QuickBar(GUIContext container, ComponentListener listener) {
		super(container, listener);
	}

	@Override
	public void render(GUIContext container, Graphics g) throws SlickException {
		if (!isVisible())
			return;
		g.setColor(back);
		g.fill(getArea());
		g.setColor(Color.white);
		g.draw(getArea());
		
		for(int i = 0;i < 10;i++)
		{
			ShortCut s = GameState.getInstance().inventory.shortcuts[i];
			if ((GameState.getInstance().inventory.constructable != -1 && s != null) && GameState.getInstance().inventory.constructable == s.getId())
				g.setColor(Color.lightGray);
			else
				g.setColor(Color.darkGray);
			if (isHorisontal())
				g.fillRect(i*36+getX()+1, getY()+3, 32, 32);
			else
				g.fillRect(getX()+2,i*36+getY()+2, 32, 32);
			
			if (s != null)
			{
				Image img = null;
				if (s.getType() == ShortCut.TYPE_ITEM)
					img = ResourceManager.getImage(DefManager.allItems.get(s.getId()).image);
				
				//if (!Config.disableLighting)
				//	LightManager.eraseBlend(img);
				
				if (isHorisontal())
					img.draw(i*36+getX()+1, getY()+3, 32, 32);
				else
					img.draw(getX()+3,i*36+getY()+2, 32, 32);
			}
			if (isHorisontal())			
				GameState.font12.drawString(i*36+getX()+1, getY()+1, String.valueOf(i+1));
			else
				GameState.font12.drawString(getX()+3, i*36+getY()+1, String.valueOf(i+1));			
		}
		g.setColor(Color.lightGray);
		g.fill(button);
		
		ResourceManager.getImage("rotate").draw(button.getX(), button.getY(), button.getWidth(), button.getHeight());
		super.render(container, g);
	}
	
	@Override
	public void setLocation(int x, int y) {
		super.setLocation(x, y);
		if (button != null)
		{
			if (isHorisontal())
				button.setLocation(getX() + GUI.iconSize*11+8, getY()+10);
			else
				button.setLocation(getX()+10,getY() + GUI.iconSize*11+8);
		}
	}
	
	@Override
	public void mouseDragged(int oldx, int oldy, int newx, int newy)
	{
		if (Math.abs(oldy-newy) <= 1 && oldx-newx == 0)
			return;
		super.mouseDragged(oldx, oldy, newx, newy);
	}
	
	@Override
	public void mouseReleased(int button, int mx, int my)
	{
		if (!isOver())
			return;
		if (getButton().contains(mx, my))
			setHorisontal(!isHorisontal());
		else
		{
			for(int i = 0;i < 10;i++)
			{
				if (isHorisontal())
				{
					if (Rectangle.contains(mx, my,i*36+getX()+1, getY()+3, 32, 32))
					{
						if (button == Input.MOUSE_LEFT_BUTTON)
							GameState.getInstance().inventory.useShorCut(i);
						else if (button == Input.MOUSE_RIGHT_BUTTON)
							GameState.getInstance().inventory.registerShortcut(null, i);
						break;
					}
				}
				else
				{
					if (Rectangle.contains(mx, my,getX()+2,i*36+getY()+2, 32, 32))
					{
						if (button == Input.MOUSE_LEFT_BUTTON)
							GameState.getInstance().inventory.useShorCut(i);
						else if (button == Input.MOUSE_RIGHT_BUTTON)
							GameState.getInstance().inventory.registerShortcut(null, i);
						break;
					}
				}
			}
		}
		super.mouseReleased(button, mx, my);
	}

	public boolean isHorisontal() {
		return horisontal;
	}

	public void setHorisontal(boolean horisontal)
	{
		this.horisontal = horisontal;
		int width = getHeight();
		int height = getWidth();
		getArea().setWidth(width);
		getArea().setHeight(height);
			setLocation(getX(),getY());
	}

	public Rectangle getButton() {
		return button;
	}

	public void setButton(Rectangle button) {
		this.button = button;
	}
	
	
	public Rectangle getArea() {
		return (Rectangle) super.getArea();
	}

	@Override
	public void update(int delta)
	{
		
	}

}
